package com.togo.common.exception;

import com.togo.common.enums.TokenType;
import com.togo.common.model.token.Token;

/**
 * @author : taiyanan
 * Date    : 2022/1/29
 * Time    : 3:27 下午
 * ---------------------------------------
 * Desc    : 解析异常
 */
public class ParseException extends RuntimeException {

    public ParseException(String message) {
        super(message);
    }

    public ParseException(Token token) {
        this("", token);
    }

    public ParseException(String message, Throwable throwable) {
        super(message, throwable);
    }

    public ParseException(String message, Token token) {
        super("syntax error around " + location(token) + ": " + message);
    }

    public ParseException(String message, Token token, Throwable cause) {
        super("syntax error around " + location(token) + ": " + message, cause);
    }

    private static String location(Token token) {
        if (TokenType.EOF == token.getType()) {
            return "the last line";
        } else {
            return "\"" + token.getValue() + "\" at line " + token.getLineNumber();
        }
    }
}
